<?php

class EmailSend_Recipient {
	public $email_address = "";
	public $recipient_type = "";
}

class EmailSend {

	public $subject = "";
	public $from_email_address = "";
	public $from_name = "";
	public $body_text = "";
	public $recipient_array = array();

	function __construct() {
		
	}

	// Check for existence of a recipient
	public function HasRecipient($emailAddress) {
		//echo "%".sizeof($this->recipient_array);
		for ($i=0; $i<sizeof($this->recipient_array); $i++) {
			//echo "@".$i;
			if (strtolower($this->recipient_array[$i]->email_address) == strtolower($emailAddress)) {
				return true;
			}
		}
		return false;
	}

	// Add recipient
	public function AddRecipient($emailAddress, $recipientType) {

		$emailAddress = trim($emailAddress) . "";
		$recipientType = trim($recipientType) . "";

		if (empty($emailAddress) || empty($recipientType)) {
			echo "<p>Error: emailAddress or recipientType missing.";
			exit();
		}

		if ($recipientType != "to" && $recipientType != "cc" && $recipientType != "bcc") {
			echo "<p>Error: recipientType is invalid.";
			exit();
		}

		// See if this address is already in the list (so we can skip
		// adding it).
		if ($this->HasRecipient($emailAddress)) {
			return false;
		}

		// Add recipient to end of recipient array.
		$temp = new EmailSend_Recipient();
		$temp->email_address = $emailAddress;
		$temp->recipient_type = $recipientType;
		array_push($this->recipient_array, $temp);
		return true;
	}


	// Add to send queue
	public function SendEmail() {
		require_once('phpmailer/class.phpmailer.php');

		//global $email_override;

		if (empty($this->subject) || empty($this->from_email_address)) {
			echo "<p>Error: required fields missing.";
			exit();
		}

		if (empty($this->body_text)) {
			echo "<p>Error: email has no body defined.";
			exit();
		}

		if (sizeof($this->recipient_array) == 0) {
			echo "<p>Error: No recipients defined.";
			exit();
		}
		
		// Send the email.
		//echo "SEND THE EMAIL HERE";
		
		$mail = new PHPMailer();
		//$mail->Mailer   = "sendmail";
		$mail->Mailer   = "smtp";
		$mail->Host     = SiteConst('smtp_host');
		$mail->Username = SiteConst('smtp_username');
		$mail->Password = SiteConst('smtp_password');
		$mail->SMTPAuth = true;
		//$mail->SMTPKeepAlive = true;

		// Add recipients.
		if (SiteConst('email_override') != "") {
			$mail->AddAddress(SiteConst('email_override'));
		}
		else {
			for ($i=0; $i<sizeof($this->recipient_array); $i++) {
				if ($this->recipient_array[$i]->recipient_type == "to")
					$mail->AddAddress($this->recipient_array[$i]->email_address);
				elseif ($this->recipient_array[$i]->recipient_type == "cc")
					$mail->AddCc($this->recipient_array[$i]->email_address);
				elseif ($this->recipient_array[$i]->recipient_type == "bcc")
					$mail->AddBcc($this->recipient_array[$i]->email_address);
				else {
					echo "<p>Error: recipientType is invalid.";
					exit();
				}
			}
		}

		$mail->From     = $this->from_email_address;
		$mail->FromName = $this->from_name;
		$mail->Subject  = $this->subject;
		if (SiteConst('email_override') != "")
			$mail->Subject .= " OVERRIDE";

		$mail->Body     = $this->body_text;

		if(!$mail->Send()) {
			echo "<p>Error sending email.  Please try again later. (?)";
			exit();
		}
	}


	// EmailChecksum()
	// If we're regenerating an identical email object through form inputs,
	// we can use this to make sure the generated object is the same.
	public function EmailChecksum() {
		$email_as_string = "";
		$email_as_string = $this->subject . "%" . $this->from_email_address . "%" . $this->from_name;
		$email_as_string .= $email_as_string . "%" . $this->body_text;
		for ($i=0; $i<sizeof($this->recipient_array); $i++) {
			$email_as_string .= "%" . $this->recipient_array[$i]->email_address . "-" . $this->recipient_array[$i]->recipient_type;
		}

		// Checksum could literally be email_as_string really, but using md5
		// to save some length.
		return md5($email_as_string);
	}


	public function GetMailToUrl() {
		
		/*$temp = "mailto:" . MailToUrlEncode(mailto)
		temp = temp & "?x=x";
		If cc <> "" Then
			temp = temp & "&cc=" & MailToUrlEncode(cc)
		End If
		If bcc <> "" Then
			temp = temp & "&bcc=" & MailToUrlEncode(bcc)
		End If
		If subject <> "" Then
			temp = temp & "&Subject=" & MailToUrlEncode(subject)
		End If
		If body <> "" Then
			temp = temp & "&Body=" & MailToUrlEncode(body)
		End If
		
		return $temp;*/
	}


	private function MailToUrlEncode($plain_text) {
		// Converts carriage returns to "%OA", &'s to "%26, quotes to something, etc.
		// I don't know what it does about pluses, because URLEncode wants to convert 
		// spaces to pluses.
		
		$temp = "";
		
		//convertTextToMailtoURL = Replace(plain_text, Chr(13) & Chr(10), "%0A")
		$temp = str_replace("+", "[p-l-u-s]", $plain_text);
		$temp = urlencode($temp);
		$temp = str_replace("+", "%20", $temp);
		$temp = str_replace("[p-l-u-s]", "+", $temp);
		
		return $temp;
	}


}

?>